package com.swpu.cbg.screenadapterdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by chenboge on 16/4/8.
 */
public class MyViewGroup extends ViewGroup {

    public MyViewGroup(Context context) {
        super(context);
    }

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthmode = MeasureSpec.getMode(widthMeasureSpec);
        int heightmode = MeasureSpec.getMode(heightMeasureSpec);

        int height = MeasureSpec.getSize(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        //通知子view测量宽高
        measureChildren(widthMeasureSpec, heightMeasureSpec);


        MarginLayoutParams mParams = null;

        int count = getChildCount();

        //获取viewgroup的宽度和高度
        int thiswidth = 0;
        int thisheight = 0;

        //子view的宽度和高度
        int cwidth = 0;
        int cheight = 0;

        View child = getChildAt(count - 1);
        cheight = child.getMeasuredHeight();
        cwidth = child.getMeasuredWidth();

        mParams = (MarginLayoutParams) child.getLayoutParams();

        thisheight = cheight + mParams.topMargin + mParams.bottomMargin;
        thiswidth = cwidth + mParams.leftMargin + mParams.rightMargin + 40;
//        System.out.println(widthmode == MeasureSpec.EXACTLY);
//        System.out.println(thiswidth);
//        System.out.println(cwidth);
//        System.out.println(heightmode == MeasureSpec.EXACTLY+thisheight);
        setMeasuredDimension((widthmode == MeasureSpec.EXACTLY) ? width : thiswidth, (heightmode == MeasureSpec.EXACTLY) ? height : thisheight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
     //   System.out.println(count);
        int cWidth = 0;
        int cHeight = 0;
        MarginLayoutParams cParams = null;
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            cWidth = child.getMeasuredWidth();
           // System.out.println("cwidth:"+cWidth);
            cHeight = child.getMeasuredHeight();
           // System.out.println("cHeight:"+cHeight);
            cParams = (MarginLayoutParams) child.getLayoutParams();
            int cl = 0, ct = 0, cr = 0, cb = 0;
            switch (i) {
                case 0:
                    cl = cParams.leftMargin + 8*35;
                    ct=cParams.topMargin+10;
                    break;

                case 1:
                    cl = cParams.leftMargin + 6*35;
                    ct=cParams.topMargin+20;
                    break;

                case 2:
                    cl = cParams.leftMargin + 4*35;
                    ct=cParams.topMargin+30;
                    break;

                case 3:
                    cl = cParams.leftMargin + 2*35;
                    ct=cParams.topMargin+40;
                    break;

            }
            cr=cl+cWidth;
            cb = ct +cHeight;
            System.out.println(cl+";"+ct+";"+cr+";"+cb);
            child.layout(cl,ct,cr,cb);
        }
    }


    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }
}
